package com.tinama.jedis01;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

/**
 * @author lgc
 * @createDate 2020/10/7 - 11:42
 */
public class Test {
    public static void main(String[] args){
        Jedis jedis = new Jedis("127.0.0.1",6379);
//        String ping = jedis.ping();
//        System.out.println(ping);
//        System.out.println("判断某个键是否存在:"+jedis.exists("redis:user:1"));
//        System.out.println("清空数据:"+jedis.flushDB());
//        System.out.println("判断某个键是否存在:"+jedis.exists("redis:user:1"));
//        System.out.println("新增key vule的键值对："+jedis.set("username","lgc"));
//        System.out.println("获取键值对截取值："+jedis.getrange("username",0,1));
//        System.out.println("获取键值对截取值："+jedis.getrange("username",0,-1));
//        System.out.println("系统中的键key："+jedis.keys("*"));
//        System.out.println("删除键key："+jedis.del("username"));
        jedis.flushDB();
        Transaction multi = jedis.multi();
        try {
            multi.set("user1","lgb");
            multi.set("user2","lgc");
            int i = 5/0;
            multi.setnx("user2","bbb");
            multi.exec();
        }catch (Exception e){
            multi.discard();
            e.printStackTrace();
        }finally {
            System.out.println(jedis.get("user1"));
            System.out.println(jedis.get("user2"));
            jedis.close();
        }


    }
}
